package com.wwy.monitor.common;



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.wwy.monitor.model.tddl.vo.MonitorTaskVo;
import com.wwy.monitor.util.DateUtil;
import com.google.common.collect.Lists;

public class DingGroupMsgContext {

    //private Message dingTalkMessage;

    private MonitorTaskVo taskVo;

    private String env;

    private String domainUrl;

    public DingGroupMsgContext(String env,String domainUrl,MonitorTaskVo taskVo){
        this.env = env;
        this.domainUrl = domainUrl;
        this.taskVo = taskVo;
        init();
    }
    private void init() {
        dingTalkMessage = new DingGroupMessage(env,domainUrl,taskVo);
    }

    public Message getDingTalkMessage() {
        return dingTalkMessage;
    }

    public void setDingTalkMessage(Message dingTalkMessage) {
        this.dingTalkMessage = dingTalkMessage;
    }

    public MonitorTaskVo getTaskVo() {
        return taskVo;
    }

    public void setTaskVo(MonitorTaskVo taskVo) {
        this.taskVo = taskVo;
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public String getDomainUrl() {
        return domainUrl;
    }

    public void setDomainUrl(String domainUrl) {
        this.domainUrl = domainUrl;
    }

    class DingGroupMessage implements Message {
        private MonitorTaskVo taskVo;

        private String env;

        private String domainUrl;

        public DingGroupMessage(String env,String domainUrl,MonitorTaskVo taskVo) {
            super();
            this.taskVo = taskVo;
            this.env = env;
            this.domainUrl = domainUrl;
        }

        @Override
        public String toJsonString() {
            StringBuilder builder = new StringBuilder("");
            builder.append("****部门-监控平台");
            builder.append("\n\n");
            builder.append("【监控任务】: " + taskVo.getTaskName());
            builder.append("\n\n");
            builder.append("【监控任务执行时间】: " + DateUtil.format(taskVo.getCreateTime(), DateUtil.SHOW_PATTERN_HMS));
            builder.append("\n\n");
            builder.append("【监控结果】: 监控结果与预期不一致");
            builder.append("\n\n");
            builder.append("【监控环境】: "+env);
            builder.append("\n\n");
            builder.append("具体信息请登录【监控平台】查看");
            builder.append("\n\n");
            Map<String, Object> content = new HashMap<>(1);
            content.put("text", builder.toString());
            content.put("hideAvatar","0");
            content.put("btnOrientation","1");
            List<Map<String,String>> list = Lists.newArrayList();
            Map<String,String> but1Map = new HashMap<>(2);
            but1Map.put("title","查看详情");
            try {
                but1Map.put("actionURL","dingtalk://dingtalkclient/page/link?url=" + URLEncoder
                        .encode(domainUrl+"/#/app/monitorlog?taskId="+ taskVo.getId(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            list.add(but1Map);
            //Map<String,String> but2Map = new HashMap<>(2);
            //but2Map.put("title","å»å®¡æ¹");
            //try {
            //    but2Map.put("actionURL","dingtalk://dingtalkclient/page/link?url=" + URLEncoder
            //        .encode(vo.getBpmsUrl() + "workdesk/instDetail?procInsId=" + vo.getProcInstId(), "UTF-8"));
            //} catch (UnsupportedEncodingException e) {
            //    e.printStackTrace();
            //}
            //list.add(but2Map);
            content.put("btns",list);
            Map<String, Object> at = new HashMap<>(1);
            at.put("isAtAll", true);
            Map<String, Object> result = new HashMap<>(3);
            result.put("msgtype", "actionCard");
            result.put("actionCard", content);
            return JSON.toJSONString(result);
        }
    }


}
